
#include "scene_page.h"
#include "page_base.h"
#include "img_common.h"
#include "sys_scene.h"
#include "pt_ipc.h"
#include "pt_info.h"
#include "sys_font.h"
#include "pt_fs.h"

static ui_scene_widget_t scene_widget;

static  char * scene_img[] =
{
    "Scene/SceneAirConditioner.png",    "Scene/SceneAirPurifier.png",   "Scene/SceneAlarmClock.png",
    "Scene/SceneBathtub.png",           "Scene/SceneBowl.png",          "Scene/SceneClean.png",
    "Scene/SceneCook.png",              "Scene/SceneCurtain.png",       "Scene/SceneDoorLock.png",
    "Scene/SceneFridge.png",            "Scene/SceneGoHome.png",        "Scene/SceneGoodMorning.png",
    "Scene/SceneHumidity.png",          "Scene/SceneLeave.png",         "Scene/SceneLight.png",
    "Scene/SceneMoon.png",              "Scene/ScenePower.png",         "Scene/SceneRead.png",
    "Scene/SceneSleep.png",             "Scene/SceneSofa.png",          "Scene/SceneTV.png",
    "Scene/SceneTemperature.png",       "Scene/SceneWarning.png",       "Scene/SceneWeather.png",
    "Scene/Scenewash.png"
 };

void ui_no_scene_page_setting(lv_obj_t *superview);
void ui_scene_page_info_update(void);

void ui_init_scene_page_src(lv_obj_t *superview)
{
    debug_d("create\n");

    scene_widget.loading_flag = 0;

    ui_scene_page_common(superview);

    ui_no_scene_page_setting(superview);
    ui_scene_page_scene(superview);
    ui_scene_page_info_update();

}

void ui_release_scene_page_src(lv_obj_t *superview)
{
    scene_widget.loading_flag = 0;

    if(scene_widget.img_bg){
        lv_obj_del(scene_widget.img_bg);
        scene_widget.img_bg = NULL;
    }

    if(scene_widget.obj_scene_collect){
        lv_obj_del(scene_widget.obj_scene_collect);
        scene_widget.obj_scene_collect = NULL;
        scene_widget.obj_scene = NULL;
        scene_widget.img_scene = NULL;
        scene_widget.img_btn = NULL;
        scene_widget.label_scene = NULL;
    }
}

static void on_page_scene_view_did_load(page_base_t* self)
{
    debug_d("scene create\n");
    ui_init_scene_page_src(self->root);
}

static void scene_page_on_view_did_unload(page_base_t* self)
{
    debug_d("scene end\n");
    com_anim_destroy();
    ui_release_scene_page_src(self->root);
}

static scene_page_on_custom_attr_config(page_base_t* self)
{}
static scene_page_on_view_load(page_base_t* self)
{}
static scene_page_on_view_will_appear(page_base_t* self)
{}
static scene_page_on_view_did_appear(page_base_t* self)
{}
static scene_page_on_view_will_disappear(page_base_t* self)
{}
static scene_page_on_view_did_disappear(page_base_t* self)
{}

static page_vtable_t page_param = {
    .on_custom_attr_config  = scene_page_on_custom_attr_config,
    .on_view_load           = scene_page_on_view_load,
    .on_view_did_load       = on_page_scene_view_did_load,
    .on_view_will_appear    = scene_page_on_view_will_appear,
    .on_view_did_appear     = scene_page_on_view_did_appear,
    .on_view_will_disappear = scene_page_on_view_will_disappear,
    .on_view_did_disappear  = scene_page_on_view_did_disappear,
    .on_view_did_unload     = scene_page_on_view_did_unload,
};

page_vtable_t* lv_page_scene_create(void)
{
    return &page_param;
}

void ui_scene_page_common(lv_obj_t *superview)
{
    lv_coord_t pw = 0;
    lv_coord_t ph = 0;

    pw = lv_disp_get_hor_res(NULL);
    ph = lv_disp_get_ver_res(NULL);
    //****************************************************** */

    scene_widget.img_bg = lv_img_create(superview);
    #if 0
    lv_img_set_src(scene_widget.img_bg, pt_fs_get("BG_Scene.png"));
    #else
    lv_img_set_src(scene_widget.img_bg, &BG_Scene);
    #endif
    
    lv_obj_set_pos(scene_widget.img_bg,0,0);
    lv_obj_set_size(scene_widget.img_bg,pw,ph);
}

void ui_no_scene_page_setting(lv_obj_t *superview)
{
    scene_widget.no_scene_img = lv_img_create(superview);
    lv_img_set_src( scene_widget.no_scene_img, pt_fs_get("NoScene.png"));
    lv_obj_set_pos(scene_widget.no_scene_img,75,74);

    scene_widget.no_scene_label = lv_label_create(superview);
    lv_obj_set_style_text_font( scene_widget.no_scene_label, lv_font_get(20), LV_STATE_DEFAULT );
    lv_obj_set_style_text_color(scene_widget.no_scene_label,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN);
    lv_obj_set_style_text_align(scene_widget.no_scene_label,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(scene_widget.no_scene_label,LV_OPA_80,0);
    lv_label_set_text( scene_widget.no_scene_label, "暂无智能场景\n前往美的美居APP -面板详情页 -屏幕设置添加");
    lv_obj_align( scene_widget.no_scene_label, LV_ALIGN_BOTTOM_MID, 0, -35 );

    if(!scene_info_get_num())
    {
        debug_d("no scene\n");
        lv_obj_clear_flag(scene_widget.no_scene_img,LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_flag(scene_widget.no_scene_label,LV_OBJ_FLAG_HIDDEN);
    }
    else
    {
        lv_obj_add_flag(scene_widget.no_scene_img,LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_flag(scene_widget.no_scene_label,LV_OBJ_FLAG_HIDDEN);       
    }
}

void ui_scene_page_info_update(void)
{
    int i = 0;
    int scene_num = 0;
    int child_cnt = 0;
    lv_obj_t* child_obj = NULL;

    if(!scene_widget.obj_scene_collect)
        return ;

    child_cnt = lv_obj_get_child_cnt(scene_widget.obj_scene_collect);
    debug_d("child_cnt=%d\n",child_cnt);

    for(i=child_cnt-1;i>=0;i--)
    {
        child_obj = lv_obj_get_child(scene_widget.obj_scene_collect,i);
        // debug_d("child_obj=%d:%x\n",i,child_obj);
        if(!child_obj){
            continue;
        }

        lv_obj_del(child_obj);
    }

    scene_num = scene_info_get_num();

    for(i=0;i<scene_num;i++)
    {
        int scene_index = 0;
        scene_info_t* info = NULL;        
        
        info = scene_info_get_byindex(i);
        if(!info){
            continue;
        }

        scene_index = atoi(info->sceneIcon);
        debug_d("scene_index=%d\n",scene_index);
        if((scene_index<0)||(scene_index>24)){
            continue;
        }     

        debug_d("sceneName=%s\n",info->sceneName);

        ui_scene_page_add_scene(scene_widget.obj_scene_collect,scene_img[scene_index],info->sceneName,i+1);
    } 
}


void ui_scene_page_scene(lv_obj_t *superview)
{
    scene_widget.obj_scene_collect = lv_obj_create(superview);
    //lv_obj_remove_style_all(scene_widget.obj_scene_collect);
    lv_obj_set_size(scene_widget.obj_scene_collect,400,220);
    lv_obj_set_pos(scene_widget.obj_scene_collect,36,60);

    lv_obj_set_style_radius(scene_widget.obj_scene_collect, 0, LV_PART_MAIN);
	lv_obj_set_style_border_width(scene_widget.obj_scene_collect, 0, LV_PART_MAIN);
    lv_obj_set_style_border_opa(scene_widget.obj_scene_collect, LV_OPA_0, LV_PART_MAIN);
	lv_obj_set_style_border_color(scene_widget.obj_scene_collect, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
    lv_obj_set_style_pad_all(scene_widget.obj_scene_collect,0,0);

    #if 1
	lv_obj_set_style_bg_opa(scene_widget.obj_scene_collect, LV_OPA_0, LV_PART_MAIN);
    #else
	lv_obj_set_style_bg_opa(scene_widget.obj_scene_collect, LV_OPA_50, LV_PART_MAIN);
	lv_obj_set_style_bg_color(scene_widget.obj_scene_collect, lv_color_make(0xFF,0,0), LV_PART_MAIN);
    #endif

}

static int ui_scene_send_http_req(int index)
{
    scene_info_t*    info = NULL; 
    scene_req_info_t scene_req;

    memset(&scene_req,0,sizeof(scene_req_info_t));

    if(scene_widget.loading_flag){
        debug_e("loading\n");
        return -1;
    }

    debug_d("index=%d\n",index);

    info = scene_info_get_byindex(index);
    if(!info){
        return -1;
    }

    scene_req.sceneId = atoi(info->sceneId);
    scene_req.homeid  = atoi(info->homeid);

    pt_ipc_send_msg(IPC_MSG_SCENE_EXEC,(void*)&scene_req,sizeof(scene_req_info_t));
    
    return 0;
}

void ui_scene_anim_timer_callback(lv_timer_t * timer)
{
    int num = 0;
    lv_obj_t* parent    = NULL;
    lv_obj_t* child_obj = NULL;

    if(!timer){
        return ;
    }

    num = (int)timer->user_data;

    debug_d("num=%d\n",num);

    if(timer){
        lv_timer_del(timer);
        timer;
    }

    if(!scene_widget.obj_scene_collect){
        return ;
    }

    debug_d("\n");

    parent = lv_obj_get_child(scene_widget.obj_scene_collect,num);
    if(!parent){
        return -1;
    }
    debug_d("\n");
    child_obj = lv_obj_get_child(parent,0);
    if(!child_obj){
        return -1;
    }
    debug_d("\n");

    lv_obj_clear_flag(child_obj,LV_OBJ_FLAG_HIDDEN);

    scene_widget.loading_flag = 0;
}

static int ui_scene_anim_done(int index)
{
    int childcnt = 0;
    lv_obj_t* parent    = NULL;
    lv_obj_t* child_obj = NULL;

    debug_d("index=%d\n",index);

    if(scene_widget.loading_flag){
        debug_e("loading\n");
        return -1;
    }

    childcnt = lv_obj_get_child_cnt(scene_widget.obj_scene_collect);
    debug_d("childcnt=%d\n",childcnt);

    parent = lv_obj_get_child(scene_widget.obj_scene_collect,index);
    if(!parent){
        return -1;
    }

    com_anim_destroy();
    com_anim_create(parent,60,5);

    child_obj = lv_obj_get_child(parent,0);
    if(!child_obj){
        return -1;
    }

    lv_obj_add_flag(child_obj,LV_OBJ_FLAG_HIDDEN);

    lv_timer_create(ui_scene_anim_timer_callback,1100,index);

    return 0;
}

static void ui_scene_id_http_exec(lv_event_t* event)
{
    int user_num = (int)lv_event_get_user_data(event);

    switch(event->code){
    case LV_EVENT_CLICKED:
    {
        debug_d("clicked\n");
        ui_scene_anim_done(user_num-1);
        ui_scene_send_http_req(user_num-1);
        scene_widget.loading_flag = 1;
        break;
    }
    default:break;
    }
}

void ui_scene_page_add_scene(lv_obj_t * superview, char * img_path, char * info,uint8_t num)
{
    scene_widget.obj_scene = lv_obj_create(scene_widget.obj_scene_collect);

    lv_obj_remove_style_all(scene_widget.obj_scene);

    lv_obj_set_style_bg_color(scene_widget.obj_scene, lv_color_hex(0xffffff), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(scene_widget.obj_scene,LV_OPA_20,LV_PART_MAIN);
    lv_obj_set_style_radius(scene_widget.obj_scene, 12, LV_PART_MAIN);
    lv_obj_set_size(scene_widget.obj_scene,180,100);

// #if 0
// 	lv_obj_set_style_bg_opa(scene_widget.obj_scene, LV_OPA_0, LV_PART_MAIN);
// #else
// 	lv_obj_set_style_bg_opa(scene_widget.obj_scene, LV_OPA_100, LV_PART_MAIN);
// 	lv_obj_set_style_bg_color(scene_widget.obj_scene, lv_color_make(0xFF, 0, 0), LV_PART_MAIN);
// #endif

    if(num%2){  //单数
        lv_obj_set_pos(scene_widget.obj_scene, 0, (num-1)/2*120);
    }else{      //双数
        lv_obj_set_pos(scene_widget.obj_scene, 200, ((num/2)-1)*120);
    }

    debug_d("img_path=%s\n",img_path);

    scene_widget.img_scene = lv_img_create(scene_widget.obj_scene);
    lv_img_set_src( scene_widget.img_scene, pt_fs_get(img_path));
    lv_obj_align( scene_widget.img_scene, LV_ALIGN_TOP_MID, 0, 5 );
    //*********************************************************** */
    scene_widget.label_scene = lv_label_create(scene_widget.obj_scene);
    lv_obj_set_style_text_font(scene_widget.label_scene, lv_font_get(20), LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(scene_widget.label_scene,LV_OPA_80,0);
    lv_label_set_text(scene_widget.label_scene, info);
    lv_obj_set_style_text_color(scene_widget.label_scene, lv_color_hex(0xffffff), LV_PART_MAIN);
   
    lv_label_set_long_mode(scene_widget.label_scene,LV_LABEL_LONG_SCROLL_CIRCULAR);
    lv_obj_set_style_text_align(scene_widget.label_scene,LV_TEXT_ALIGN_CENTER,0);
    
    lv_obj_set_pos(scene_widget.label_scene,20,70);
    lv_obj_set_size(scene_widget.label_scene,140,26);

#if 1
	lv_obj_set_style_bg_opa(scene_widget.label_scene, LV_OPA_0, LV_PART_MAIN);
#else
	lv_obj_set_style_bg_opa(scene_widget.label_scene, LV_OPA_100, LV_PART_MAIN);
	lv_obj_set_style_bg_color(scene_widget.label_scene, lv_color_make(0,0,0), LV_PART_MAIN);
#endif
    //************************************************************** */
    scene_widget.img_btn = lv_obj_create(scene_widget.obj_scene);

    lv_obj_set_pos(scene_widget.img_btn,55,0);
    lv_obj_set_size(scene_widget.img_btn,70,60);

    lv_obj_set_style_radius(scene_widget.img_btn, 0, LV_PART_MAIN);
    lv_obj_set_style_border_width(scene_widget.img_btn, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(scene_widget.img_btn, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(scene_widget.img_btn, LV_OPA_0, LV_PART_MAIN);

#if 1
	lv_obj_set_style_bg_opa(scene_widget.img_btn, LV_OPA_0, LV_PART_MAIN);
#else
	lv_obj_set_style_bg_opa(scene_widget.img_btn, LV_OPA_100, LV_PART_MAIN);
	lv_obj_set_style_bg_color(scene_widget.img_btn, lv_color_make(0,0,0xff), LV_PART_MAIN);
#endif

    lv_obj_add_event_cb(scene_widget.img_btn,ui_scene_id_http_exec,LV_EVENT_ALL,num);
    //**************************************************************

}

static void scene_on_page_create(lv_obj_t* parent,void* ui_data)
{
    debug_d("create\n");
    ui_init_scene_page_src(parent);

}
static void scene_on_page_destroy(void* ui_data)
{
    debug_d("destroy\n");
    com_anim_destroy();
    ui_release_scene_page_src(NULL);
}
static void scene_on_page_update(void* ui_data)
{

}
static page_method_t page_scene = {
    .on_page_create  = scene_on_page_create,
    .on_page_destroy = scene_on_page_destroy,
    .on_page_update  = scene_on_page_update,
};

page_method_t* lv_scene_page_method(void)
{
    return &page_scene;
}
